package org.wyf.EveryPoint.crowd.service.impl;

import org.wyf.EveryPoint.common.entity.QueryRequest;

import org.wyf.EveryPoint.crowd.entity.Project;
import org.wyf.EveryPoint.crowd.mapper.ProjectMapper;
import org.wyf.EveryPoint.crowd.service.IProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * 项目表 Service实现
 *
 * @author wyf
 * @date 2021-04-09 16:56:53
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    private final ProjectMapper projectMapper;

    @Override
    public IPage<Project> findProjects(QueryRequest request, Project project,List<Long> ids) {
        // 只能查询所属账号下的项目
        Page<Project> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.baseMapper.findProjects(page,project,ids);
        return page;
    }


    @Override
    public List<Project> findProjects(Project project) {
	    LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createProject(Project project) {
        this.save(project);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProject(Project project) {
        this.saveOrUpdate(project);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProject(Project project) {
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    public List<Project> getBanner() {
        return this.baseMapper.getBanner();
    }

    /**
     * 创建一个项目
     * @param project
     * @return
     */
    @Override
    public Long creatProject(Project project) {
        // 这儿是创建一个项目的基本信息
        // 标题 短标题  简介  类别   开始时间  结束时间 目标金额 ==> 表单信息
        // uid  发起人信息   状态 0 未提交
        project.setState(0);
        project.setSubmitCount(0);
        this.baseMapper.creatProject(project);
        return project.getProjectId();
    }

    @Override
    public List<Map> countProjectOfType() {
        return this.baseMapper.countProjectOfType();
    }

    @Override
    public List<Project> getIndexShow() {
        return this.baseMapper.getIndexShow();
    }

    @Override
    public IPage<?> getProjectList(QueryRequest queryRequest, String keyWord, String sortType) {
        Page<Project> page = new Page<>(queryRequest.getPageNum(),queryRequest.getPageSize());
        if ("fundFinish".equals(sortType)){
            page = this.baseMapper.getFinishProjectList(page,keyWord,sortType);
        }else {
            page = this.baseMapper.getProjectList(page,keyWord,sortType);
        }
        return page;
    }

    @Override
    public Project selectProjectByOrderId(String orderid) {
        return this.baseMapper.selectProjectByOrderId(orderid);
    }

    @Override
    public IPage<?> getCollectionProjectList(QueryRequest queryRequest, Long userId) {
        Page<Project> page = new Page<>(queryRequest.getPageNum(),queryRequest.getPageSize());
       page = this.baseMapper.getCollectionProjectList(page,userId);
       return page;
    }

    @Override
    public List<Project> getUserAllprojectByUid(Long userId) {
        return this.baseMapper.getUserAllprojectByUid(userId);
    }

    @Override
    public List<Project> getUserProjectByState(Long userId, Integer state) {
        return this.baseMapper.getUserProjectByState(userId,state);
    }
}
